package com.datastructure.test.convertbst;

public class ConvertBST {

    int sum = 0;
    public TreeNode convertBST (TreeNode root) {
        if (root == null) return null;
        dfs(root);
        return root;
    }

    private void dfs(TreeNode root) {
        if (root != null){
            dfs(root.right);
            sum+=root.val;
            root.val = sum;
            dfs(root.left);
        }
    }
}

class TreeNode {
   int val = 0;
   TreeNode left = null;
   TreeNode right = null;
   public TreeNode(int val) {
     this.val = val;
   }
}
